package com.wanjia.bpsapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanjia.bpsapi.annotation.LoginToken;
import com.wanjia.bpsapi.exception.ControllerException;
import com.wanjia.bpsapi.pojo.dto.UserDto;
import com.wanjia.bpsapi.pojo.dto.UserVo;
import com.wanjia.bpsapi.pojo.entity.User;
import com.wanjia.bpsapi.pojo.model.JsonResult;
import com.wanjia.bpsapi.service.IUserService;
import com.wanjia.bpsapi.service.TokenService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


//用户登陆接口
@RestController
@Tag(name = "用户登陆接口控制器")
public class UserController extends BaseController{

    @Autowired
    IUserService userService;

    @Autowired
    TokenService tokenService;


    //用户登陆接口
    @PostMapping("/login")
    @Operation(summary = "用户登陆接口")
    public JsonResult<Object> login(@RequestBody User user) {
        User user1 = userService.getUser(user);
        String token = tokenService.getToken(user1);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("token",token);
        UserDto userDto = new UserDto();
        userDto.setId(user1.getId());
        userDto.setRole(user1.getRole());
        userDto.setUsername(user1.getUsername());
        userDto.setPhone(user1.getPhone());
        userDto.setStatus(user1.getStatus());
        jsonObject.put("data",userDto);
        return new JsonResult<>(OK,jsonObject);
    }

    @PostMapping("/user/add")
    @Operation(summary = "用户新增接口")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult addUser(@RequestBody User user){
        userService.addUser(user);
        return new JsonResult(OK);
    }

    @PutMapping("/user/update")
    @Operation(summary = "用户更新接口")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult updateUser(@RequestBody UserVo user){
        userService.updateUser(user);
        return new JsonResult(OK);
    }

    @DeleteMapping("/user/delete")
    @Operation(summary = "用户删除接口")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult deteleUser(int id){
        boolean b = userService.removeById(id);
        if (b==false){
            throw new ControllerException("删除用户失败");
        }
        return new JsonResult(OK);
    }


    @GetMapping("/user/list")
    @Operation(summary = "获取用户列表接口")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult<List<User>> getList(String username,Integer pageNo, Integer pageSize){
        Page<UserDto> page = new Page<>(pageNo, pageSize);
        List<User> data = userService.getUserList(username,pageNo,pageSize);
        List<UserDto> userDtoList =new ArrayList<>();
        for (User datum : data) {
            UserDto userDto = new UserDto();
            userDto.setId(datum.getId());
            userDto.setRole(datum.getRole());
            userDto.setUsername(datum.getUsername());
            userDto.setPhone(datum.getPhone());
            userDto.setStatus(datum.getStatus());
            userDtoList.add(userDto);
        }
        int size = data.size();
        page.setTotal(size);
        long pages = size / pageSize;
        if (size - pages * pageSize > 0) {
            pages++;
        }
        page.setRecords(userDtoList);
        page.setPages(pages);
        return new JsonResult(OK,page);
    }


}
